<?php

namespace app\index\service;

use app\index\model\AccountModel;
use app\index\model\ArchiveModel;
use app\index\model\BidModel;
use app\index\model\CarModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertUseModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginModel;
use app\index\model\HouJieModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\validate\CertUse;

class FactorService
{

    public function bid($factor, $minfo){
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($minfo['gui_unit']);
        $minfo['tou_unit_name'] = DeptModel::get_name_by_pk($minfo['tou_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function bid_book($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['maker_name'] = UserModel::get_realname_by_pk($minfo['maker']);
        return $this->check_valid($factor, $minfo);
    }
    public function qian_shou($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function qian_ban($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function qian_zhi($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['fulei_name'] = (new OptService())->get_base_showname('cai_qianzhifulei', $minfo['fulei']);
        return $this->check_valid($factor, $minfo);
    }
    public function qian_piao($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function tou_jiao($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['ziqu_name'] = (new QianService())->get_tou_jiao_ziqu_name($minfo['ziqu']);
        return $this->check_valid($factor, $minfo);
    }
    public function tou_fan($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function tou_han($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }














    public function dealcom($factor, $minfo){
        $minfo['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $minfo['type']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_jie($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function hou_shou($factor, $minfo){
        $minfo['chuer'] = HouJieModel::where(['id'=>$minfo['jieid']])->value('chuer');
        return $this->check_valid($factor, $minfo);
    }
    public function hou_tiao($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function hou_bao($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_dayshou($factor, $minfo){
        $minfo['shouqilei_name'] = (new OptService())->get_base_showname('cai_shouqilei', $minfo['shouqilei']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_dayzhi($factor, $minfo){
        $minfo['feilei_name'] = (new OptService())->get_base_showname('cai_dayzhifeilei', $minfo['feilei']);
        return $this->check_valid($factor, $minfo);
    }
    public function piao_day($factor, $minfo){
        $minfo['kuantype_name'] = (new OptService())->get_base_showname('cai_kuantype', $minfo['kuantype']);
        return $this->check_valid($factor, $minfo);
    }
    public function coss_jie($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_employ($factor, $minfo){
        $minfo['person_nature_name'] = (new OptService())->get_base_showname('person_nature', $minfo['person_nature']);

        $qualification = array_filter(explode(',',$minfo['qualification']));
        $data['qualification_name'] = '';
        foreach ($qualification as $v){
            $data['qualification_name'] .= empty($data['qualification_name']) ? (new OptService())->get_base_showname('cert_employ_qualification', $v)
                : '，'.(new OptService())->get_base_showname('cert_employ_qualification', $v);
        }

        return $this->check_valid($factor, $minfo);
    }
    public function contract_xing($factor, $minfo){
        $minfo['caitype_name'] = (new OptService())->get_base_showname('engin_xing_caitype', $minfo['caitype']);
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($minfo['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }


















    public function cert_borrow($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_use_bid($factor, $minfo){
        $engin = BidModel::get_info_by_pk($minfo['bidid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function cert_use($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function cert_down($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }


















    public function engin($factor, $minfo){
        $minfo['engin_name'] = $minfo['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($minfo['gui_unit']);
        $minfo['lantake_name'] = (new EnginService())->get_engin_lantake_name($minfo['lantake']);
        $minfo['state_name'] = (new EnginService())->get_engin_state_name($minfo);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_nei($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_qu($factor, $minfo){
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($minfo['gui_unit']);
        $minfo['qiantype_name'] = (new OptService())->get_base_showname('contract_nei_qiantype', $minfo['qiantype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_shi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $minfo['shihetype']);
        $minfo['chenger_name'] = DeptModel::get_name_by_pk($minfo['chenger']);
        return $this->check_valid($factor, $minfo);
    }
    public function kaoshi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function kaobao($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gofuer'] = $engin['gofuer'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_bu($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $chenger = ContractShiModel::where(['id'=>$minfo['shiid']])->value('chenger');
        $minfo['chenger_name'] = DeptModel::get_name_by_pk($chenger);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_shi_jie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_cai($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['caitype_name'] = (new OptService())->get_base_showname('engin_cai_caitype', $minfo['caitype']);
        $minfo['caier_name'] = DeptModel::get_name_by_pk($minfo['caier']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_cai_jie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $caiinfo = ContractCaiModel::where(['id'=>$minfo['caiid']])->find();
        $minfo['caitype_name'] = (new OptService())->get_base_showname('engin_cai_caitype', $caiinfo['caitype']);
        $minfo['caier_name'] = DeptModel::get_name_by_pk($caiinfo['caier']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_gonghui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['shougonglei_name'] = (new OptService())->get_base_showname('cai_shougonglei', $minfo['shougonglei']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_gongyu($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function hou_gongzhi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_gongshui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['tuifang_name'] = (new HouService())->get_hou_gongshui_tuifang_name($minfo['tuifang']);
        return $this->check_valid($factor, $minfo);
    }
    public function engin_shui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function piao_kai($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['kaier_name'] = DeptModel::get_name_by_pk($minfo['kaier']);
        $minfo['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $minfo['qianpiaotype']);
        return $this->check_valid($factor, $minfo);
    }
    public function engin_kua($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function piao_shou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        return $this->check_valid($factor, $minfo);
    }
    public function engin_stamp($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['zitype'] = (new OptService())->get_base_showname('scene_zitype', $minfo['zitype']);
        return $this->check_valid($factor, $minfo);
    }
    public function engin_cha($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        $minfo['chatype_name'] = (new OptService())->get_base_showname('engin_chatype', $minfo['chatype']);
        return $this->check_valid($factor, $minfo);
    }
    public function doss_jie($factor, $minfo){
        $minfo['jieer_name'] = UserModel::get_realname_by_pk($minfo['jieer']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_chu($factor, $minfo){
        $minfo['caitype_name'] = (new OptService())->get_base_showname('engin_cai_caitype', $minfo['caitype']);
        $minfo['hetype_name'] = (new ContractService())->get_contract_hetype_name($minfo['hetype']);
        $minfo['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $minfo['qianpiaotype']);
        $minfo['caier_name'] = DeptModel::get_name_by_pk($minfo['caier']);
        return $this->check_valid($factor, $minfo);
    }
    public function hou_chushui($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function contract_chu_jie($factor, $minfo){
        $minfo['lianer'] = ContractChuModel::where(['id'=>$minfo['chuid']])->value('lianer');
        return $this->check_valid($factor, $minfo);
    }
    public function hou_chujin($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function piao_chu($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function account_diao($factor, $minfo){
        $minfo['chuid_name'] = AccountModel::where(['id'=>$minfo['chuid']])->value('name');
        $minfo['ruid_name'] = AccountModel::where(['id'=>$minfo['ruid']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function kuai_ji($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['jier_name'] = UserModel::get_realname_by_pk($minfo['jier']);
        return $this->check_valid($factor, $minfo);
    }
    public function kuai_shou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        $minfo['jier_name'] = UserModel::get_realname_by_pk($minfo['jier']);
        return $this->check_valid($factor, $minfo);
    }
    public function wu_cai($factor, $minfo){
        $minfo['cailei_name'] = (new OptService())->get_base_showname('wu_cailei', $minfo['cailei']);
        return $this->check_valid($factor, $minfo);
    }
    public function wu_ling($factor, $minfo){
        $minfo['cangku_name'] = (new OptService())->get_base_showname('wu_cangku', $minfo['cangku']);
        return $this->check_valid($factor, $minfo);
    }
    public function book_jie($factor, $minfo){
        $minfo['jieer_name'] = UserModel::get_realname_by_pk($minfo['jieer']);
        return $this->check_valid($factor, $minfo);
    }
    public function xoss_jie($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function wu_ru($factor, $minfo){
        $minfo['rulei_name'] = (new OptService())->get_base_showname('wu_rulei', $minfo['rulei']);
        $minfo['cangku_name'] = (new OptService())->get_base_showname('wu_cangku', $minfo['cangku']);
        return $this->check_valid($factor, $minfo);
    }
    public function port($factor, $minfo){
        $minfo['huilei_name'] = (new OptService())->get_base_showname('port_huilei', $minfo['huilei']);
        return $this->check_valid($factor, $minfo);
    }
    public function peng($factor, $minfo){
        $minfo['gouxing_name'] = (new OptService())->get_base_showname('peng_gouxing', $minfo['gouxing']);
        return $this->check_valid($factor, $minfo);
    }
    public function notice($factor, $minfo){
        $minfo['tongtype_name'] = (new XingService())->get_notice_tongtype_show($minfo['tongtype']);
        $minfo['facom_name'] = DeptModel::get_name_by_pk($minfo['facom']);
        return $this->check_valid($factor, $minfo);
    }
    public function qianfa($factor, $minfo){
        $minfo['wenlei_name'] = (new OptService())->get_base_showname('qianfa_wenlei', $minfo['wenlei']);
        return $this->check_valid($factor, $minfo);
    }
    public function wagyuan($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function wagjian($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function tongyong($factor, $minfo){
        $minfo['shilei'] = (new OptService())->get_base_showname('qi_tongyong_shilei', $minfo['shilei']);
        return $this->check_valid($factor, $minfo);
    }
    public function meet($factor, $minfo){
        $minfo['room'] = (new OptService())->get_base_showname('qi_meet_room', $minfo['room']);
        return $this->check_valid($factor, $minfo);
    }
    public function qian_suo($factor, $minfo){
        $minfo['state_name'] = (new ContractService())->get_contract_nei_state_name($minfo['state']);
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        if($engin){
            $minfo['engin_name'] = $engin['name'];
            $minfo['gui_unit_name'] = DeptModel::get_name_by_pk($engin['gui_unit']);
        }else{
            $minfo['engin_name'] = '';
            $minfo['gui_unit_name'] = '';
        }
        return $this->check_valid($factor, $minfo);
    }
    public function seal_gai($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function seal_wai($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function seal_zhi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['engin_name'] = $engin['name'];
        return $this->check_valid($factor, $minfo);
    }
    public function car_jia($factor, $minfo){
        $carinfo = CarModel::get_info_by_pk($minfo['carid']);
        $minfo['paihao'] = $carinfo['paihao'];
        return $this->check_valid($factor, $minfo);
    }
    public function car_xi($factor, $minfo){
        $carinfo = CarModel::get_info_by_pk($minfo['carid']);
        $minfo['paihao'] = $carinfo['paihao'];
        return $this->check_valid($factor, $minfo);
    }
    public function car_bao($factor, $minfo){
        $carinfo = CarModel::get_info_by_pk($minfo['carid']);
        $minfo['paihao'] = $carinfo['paihao'];
        return $this->check_valid($factor, $minfo);
    }














    private function check_valid($factor, $minfo){
        $result = false;
        foreach ($factor as $fac){
            $fres = true;
            foreach ($fac as $item){
                if(!$this->check_field_factor($minfo[$item['fieldname']], $item['fieldtype'], $item['fieldvalue'])){
                    $fres = false; continue;
                }
            }
            if($fres){
                $result = true; break;
            }
        }
        return $result;
    }
    private function check_field_factor($value, $conder, $convalue){
        if($conder == 'like'){
            if(strpos($value, $convalue) !== false){
                return true;
            }
        }elseif($conder == 'notlike'){
            if(strpos($value, $convalue) === false){
                return true;
            }
        }elseif($conder == 'eq'){
            if($value == $convalue){
                return true;
            }
        }elseif($conder == 'neq'){
            if($value != $convalue){
                return true;
            }
        }elseif($conder == 'gt'){
            if($value > $convalue){
                return true;
            }
        }elseif($conder == 'egt'){
            if($value >= $convalue){
                return true;
            }
        }elseif($conder == 'lt'){
            if($value < $convalue){
                return true;
            }
        }elseif($conder == 'elt'){
            if($value <= $convalue){
                return true;
            }
        }

        return false;
    }

}
